/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ipfw.cs590.cheat.struts.actions;

import edu.ipfw.cs590.cheat.AssignmentManagementInterface;
import edu.ipfw.cs590.cheat.AssignmentManagementService;
import edu.ipfw.cs590.cheat.struts.actionforms.AssignmentManagementForm;
import edu.ipfw.cs590.cheat.util.Authentication;
import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author SafarTimuro
 */
public class SubmitAssignment extends Action {

    private final static String ADD_SUCCESS = "AddSuccess";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        //If the form is a pending user management form
        if (form instanceof AssignmentManagementForm) {
            //Get the assignment Management form
            AssignmentManagementForm amf = (AssignmentManagementForm) form;

            //Get a sesion if one doesn't exist
            HttpSession session = request.getSession(false);

            //if we have a session
            if (session != null) {
                try {
                    // Call Web Service Operation
                    AssignmentManagementService service = new AssignmentManagementService();
                    AssignmentManagementInterface port = service.getAssignmentManagementSOAP();
                    // TODO initialize WS operation arguments here
                    edu.ipfw.cs590.cheat.SubmitAssignment parameters = new edu.ipfw.cs590.cheat.SubmitAssignment();
                    parameters.setArchive(new JAXBElement(new QName("Archive"),
                            amf.getFile().getFileData().getClass(),
                            amf.getFile().getFileData()));
                    String token = (String) session.getAttribute(Authentication.SESSION_NAME_TOKEN);
                    // TODO process result here
                    String result = port.submitAssignment(parameters, token).getResult();

                    if (result != null) {
                        //submission success
                        ActionMessages errors = new ActionMessages();
                        errors.add(ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage("success.service.submitAssignment"));
                        this.saveMessages(request, errors);

                        return mapping.findForward(ADD_SUCCESS);
                    }
                } catch (Exception exc) {
                    //Something bad happened on the webservice side fail.
                    ActionMessages errors = new ActionMessages();
                    errors.add(ActionMessages.GLOBAL_MESSAGE,
                            new ActionMessage("errors.service.error", "Submit Assignment", exc.getMessage()));
                    this.saveErrors(request, errors);

                    //Return failure.
                    return mapping.getInputForward();
                }
            }
            //no session
            ActionMessages errors = new ActionMessages();
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.service.nosession"));
            this.saveMessages(request, errors);

            return mapping.getInputForward();
        }

        //Something bad happened on the webservice side fail.
        ActionMessages errors = new ActionMessages();
        errors.add(ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("error.form.invalid"));
        this.saveErrors(request, errors);

        return mapping.getInputForward();
    }
}
